//
// $Id: Promise.as 4 2010-04-20 17:58:18Z ray.j.greenwell $
//
// Flash Utils library - general purpose ActionScript utility code
// Copyright (C) 2007-2010 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/ooolib/
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

package com.threerings.util {

/**
 * Contains a value which may not be ready yet. If you request the value with get(), we'll promise
 * to get back to you eventually, or immediately if the value is already available.
 */
public class Promise
{
    public function get (f :Function) :void
    {
        if (_callbacks != null) {
            _callbacks.push(f);
        } else {
            f(_value);
        }
    }

    public function set (value :*) :void
    {
        _value = value;

        if (_callbacks != null) {
            for each (var f :Function in _callbacks) {
                f(value);
            }
            _callbacks = null; // Promise fulfilled
        }
    }

    public function get ready () :Boolean
    {
        return (_callbacks == null);
    }

    protected var _value :*;
    protected var _callbacks :Array = [];
}

}
